package im.starchat.chat.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import im.starchat.chat.R;
import im.starchat.chat.adapter.SmartFragmentStatePagerAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class MainTabChannelFragment extends Fragment implements OnClickListener {

    @InjectView(R.id.channel_viewpager)
    ViewPager mChannelViewPager;
    @InjectView(R.id.channel_popularity)
    TextView mPopularityChannel;
    @InjectView(R.id.channel_newest)
    TextView mNewestChannel;


    ChannelFragment mPopularityFragment;
    ChannelFragment mNewestFragment;
    private int mCurrentTabID;

    ChannelPagerAdapter mChannelAdapter;
    List<Fragment> channelFragmentList;
    MainChannelPagerOnPageChangeListener mMainChannelPagerOnPageChangeListener;

    public static MainTabChannelFragment newInstance() {
        return new MainTabChannelFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        channelFragmentList = new ArrayList<>();
        mPopularityFragment = ChannelFragment.newInstance("hot");
        channelFragmentList.add(mPopularityFragment);
        mNewestFragment = ChannelFragment.newInstance("newest");
        channelFragmentList.add(mNewestFragment);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_plaza, container, false);
        ButterKnife.inject(this, view);

        mPopularityChannel.setOnClickListener(this);
        mNewestChannel.setOnClickListener(this);

        mChannelAdapter = new ChannelPagerAdapter(getChildFragmentManager(), channelFragmentList);
        mChannelViewPager.setAdapter(mChannelAdapter);

        mMainChannelPagerOnPageChangeListener = new MainChannelPagerOnPageChangeListener();
        mChannelViewPager.setOnPageChangeListener(mMainChannelPagerOnPageChangeListener);
        mChannelViewPager.setCurrentItem(0);

        return view;
    }

    public class MainChannelPagerOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
//            favTypeHost.check(position == 0 ? R.id.fav_topic : R.id.fav_comic);
            //TODO 主界面接口回调

            //如果是关注列表页
            if (position == 0) {

                mCurrentTabID = R.id.channel_popularity;
                mPopularityChannel.setBackgroundResource(R.drawable.bg_rounded_feed_tab);
                mPopularityChannel.setTextColor(getResources().getColor(R.color.color_black));
                mNewestChannel.setBackground(null);
                mNewestChannel.setTextColor(getResources().getColor(R.color.color_text_5));

            } else {
                mCurrentTabID = R.id.channel_newest;
                mNewestChannel.setBackgroundResource(R.drawable.bg_rounded_feed_tab);
                mNewestChannel.setTextColor(getResources().getColor(R.color.color_black));
                mPopularityChannel.setBackground(null);
                mPopularityChannel.setTextColor(getResources().getColor(R.color.color_text_5));
            }

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    public static class ChannelPagerAdapter extends SmartFragmentStatePagerAdapter {
        List<Fragment> fragmentList;

        public ChannelPagerAdapter(android.support.v4.app.FragmentManager fragmentManager, List<Fragment> fragments) {
            super(fragmentManager);
            this.fragmentList = fragments;
        }

        @Override
        public Fragment getItem(int position) {

            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.channel_popularity:
                if(mChannelViewPager != null){
                    mChannelViewPager.setCurrentItem(0);
                }
                break;
            case R.id.channel_newest:
                if(mChannelViewPager != null){
                    mChannelViewPager.setCurrentItem(1);
                }
                break;
        }
    }

}
